package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/member")
public class MemberManagementController {

    @Reference
    private MemberService memberService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result memberfindPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = this.memberService.SearchInformation(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }

    }


    /**
     * 添加会员
     *
     * @param member
     * @return
     */
    @PostMapping("/add")
    public Result memberadd(@RequestBody Member member) {
        try {
            this.memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 编辑时查询当前id的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result popup(@PathVariable("id") Integer id) {
        try {
            Member member = this.memberService.selectbyid(id);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }


    /**
     * 修改会员信息
     *
     * @param member
     * @return
     */
    @PutMapping("/update")
    public Result editing(@RequestBody Member member) {
        try {
            this.memberService.update(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }


    /**
     * 修改启用停用状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/{status}")
    public Result ed(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {

        try {
            this.memberService.updatestatus(id,status);
            return new Result(true, MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
        }
    }

}
